//
//  TTTotalOrderViewController.m
//  TaoTuo
//
//  Created by 刘广 on 2020/3/23.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTTotalOrderViewController.h"

#import "TTShopCartViewController.h"
#import "TTMyOrderViewController.h"
#import "TTDianpuOrderManagerViewController.h"

#import "LLListView.h"

@interface TTTotalOrderViewController ()<JXCategoryViewDelegate, JXCategoryListContainerViewDelegate, JXCategoryListContentViewDelegate>
{
    UIBarButtonItem *rightBarButtonItem;
}

@property (nonatomic, strong) JXCategoryTitleView *categoryTitleView;
@property (nonatomic, strong) NSArray *titles;

@end

@implementation TTTotalOrderViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"threedot"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(showPopViewAction)];
    
    // 开通店铺成功
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupSubviews) name:openDianpuSuccessNotifyKey object:nil];
    
    @weakify(self)
    [[TTLoginSingle sharedSingle].loginSubject subscribeNext:^(NSNumber * _Nullable num) {
        @strongify(self)
        [self setupSubviews];
    }];
    
    
    [self setupSubviews];
}

- (void)showPopViewAction
{
    CGRect rect = CGRectMake(self.view.width - 130, StatusBarAndNavBar_Height, 120, 86);
    [LLListView showWithFrame:rect Images:@[@"lajitong", @"lajitong"] Titles:@[@"删除", @"清空已售"] MenuType:ListViewMenuType_ScaleBasedTopRight ClickItem:^(NSInteger index, NSString *title) {
        if (index == 0) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"shopCart_delete_notify_key" object:nil];
        } else if (index == 1) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"shopCart_removeSellout_notify_key" object:nil];
        }
    } triangleViewSpaceRight:40];
}

#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index
{
    BOOL isVip = [TTLoginSingle sharedSingle].userModel.VipLevel == 1;
    NSInteger shopIndex = isVip ? 1 : 0;
    self.navigationItem.rightBarButtonItem = index == shopIndex ? rightBarButtonItem : nil;
}

- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index {
//    self.curretnIndex = index;
    [self.listContainerView didClickSelectedItemAtIndex:index];
}

- (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio {
    [self.listContainerView scrollingFromLeftIndex:leftIndex toRightIndex:rightIndex ratio:ratio selectedIndex:categoryView.selectedIndex];
}

#pragma mark - JXCategoryListContainerViewDelegate

- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    
    BOOL isVip = [TTLoginSingle sharedSingle].userModel.VipLevel == 1;
    if (isVip) {
        if (index == 0) {
            TTDianpuOrderManagerViewController *omVC =[[TTDianpuOrderManagerViewController alloc] initWithDefaultIndex:0];
            omVC.bottomSpace = Tabbar_Height;
            return omVC;
            
        } else if (index == 1) {
            return [[TTShopCartViewController alloc] init];
        } else {
            TTMyOrderViewController *VC =[[TTMyOrderViewController alloc] init];
            VC.bottomSpace = Tabbar_Height;
            return VC;
        }
    } else {
        if (index == 0) {
            return [[TTShopCartViewController alloc] init];
        } else {
            TTMyOrderViewController *VC =[[TTMyOrderViewController alloc] init];
            VC.bottomSpace = Tabbar_Height;
            return VC;
        }
    }
    
}

- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}


- (UIView *)listView
{
    return self.view;
}


- (void)setupSubviews
{
    for (UIView *subView in self.view.subviews) {
        [subView removeFromSuperview];
    }
    
    BOOL isVip = [TTLoginSingle sharedSingle].userModel.VipLevel == 1;
    self.titles = isVip ? @[@"销售订单", @"购物车", @"采购订单"] : @[@"购物车", @"采购订单"];
    self.categoryTitleView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, 290, 44)];
    self.categoryTitleView.delegate = self;
    self.categoryTitleView.titles = self.titles;
    [self.view addSubview:self.categoryTitleView];
//    self.categoryTitleView.contentEdgeInsetLeft = 20;        // 左边距
//    self.categoryTitleView.contentEdgeInsetRight = 20;       // 右边距
    self.categoryTitleView.cellSpacing = 13;
    self.categoryTitleView.titleSelectedColor = UIColorHex(333333);
    self.categoryTitleView.titleColor = UIColorHex(333333);
    self.categoryTitleView.titleLabelZoomEnabled = NO;
    
    [self.categoryTitleView removeFromSuperview];
    self.navigationItem.titleView = self.categoryTitleView;
    
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    [lineView removeFromSuperview];
    lineView.indicatorColor = UIColorHex(9B71FF);
    lineView.indicatorHeight = 8;
    lineView.verticalMargin = 10;
    lineView.indicatorCornerRadius = 4;
    lineView.lineStyle = JXCategoryIndicatorLineStyle_Lengthen;
    self.categoryTitleView.indicators = @[lineView];
    
    
    [_listContainerView removeFromSuperview];
    _listContainerView = [[JXCategoryListContainerView alloc] initWithDelegate:self];
    _listContainerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavBar_Height - Tabbar_Height);
    [self.view addSubview:_listContainerView];
    
    
    self.categoryTitleView.contentScrollView = self.listContainerView.scrollView;
    
    self.categoryTitleView.defaultSelectedIndex = self.titles.count - 1;
    self.listContainerView.defaultSelectedIndex = self.titles.count - 1;
    
    [self.categoryTitleView reloadData];
    [_listContainerView reloadData];
    
    self.navigationItem.rightBarButtonItem = nil;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
